package com.yangtzeu.jwc.activity;

import java.io.Serializable;
import java.util.List;
import java.util.Map;

import android.annotation.SuppressLint;
import android.app.ProgressDialog;
import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v7.app.AlertDialog;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

import com.yangtzeu.jwc.app.MyApplication;
import com.yangtzeu.jwc.businessservice.BusinessHttpCallBack;
import com.yangtzeu.jwc.businessservice.impl.StudentServiceImpl;
import com.yangtzeu.jwc.customview.CustomeProgressDialog;
import com.yangtzeu.jwc.domain.Course;
import com.yangtzeu.jwc.domain.XkInfo;
import com.zhangdi.jwc.R;
import com.zjsx.lib.utils.L;

public class XkActivity extends BaseActivity {
    private Button selYear;
    private Button selTerm;
    private Button selXiaoqu;
    private TextView lbGxMax;
    private TextView lbZyMax;
    private TextView lbGxxf;
    private TextView lbGxbjgxf;
    private TextView lbGx;
    private TextView lbGxCount;
    private TextView lbGxMaxCount;
    private TextView lbZyxf;
    private TextView lbZybjgxf;
    private TextView lbZy;
    private Handler myHandler;
    private ProgressDialog dialog;
    private XkInfo xkInfo;
    @SuppressWarnings("rawtypes")
    private Map gxkInfo;
    private List<Course> courses;
    private static final int SHOWPROGRESSDIALOG = 0;
    private static final int DISMISSDIALOG = 1;
    private static final int LOADINGFAILED = 2;
    private static final int LOADINGXKINFOFINISHED = 3;
    private static final int LOADINGGXKLISTFINISHED = 4;
    private static final int LOADINGSELECTEDCOURCEFINISHED = 5;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        myHandler = new MyHandler();
        getXkInfo();
    }

    @Override
    public void setView() {
        super.setView();
        setContentView(R.layout.activity_xk);
    }

    @Override
    public void initActionBar() {
        setSimpleActionBar(null, "我的选课信息", null, null);
    }

    @Override
    public void initView() {
        super.initView();
        selTerm = (Button) findViewById(R.id.selTerm);
        selYear = (Button) findViewById(R.id.selYear);
        selXiaoqu = (Button) findViewById(R.id.selXiaoqu);
        lbGxMax = (TextView) findViewById(R.id.lbGxMax);
        lbZyMax = (TextView) findViewById(R.id.lbZyMax);
        lbGxxf = (TextView) findViewById(R.id.lbGxxf);
        lbGxbjgxf = (TextView) findViewById(R.id.lbGxbjgxf);
        lbGx = (TextView) findViewById(R.id.lbGx);
        lbGxCount = (TextView) findViewById(R.id.lbGxCount);
        lbGxMaxCount = (TextView) findViewById(R.id.lbGxMaxCount);
        lbZyxf = (TextView) findViewById(R.id.lbZyxf);
        lbZybjgxf = (TextView) findViewById(R.id.lbZybjgxf);
        lbZy = (TextView) findViewById(R.id.lbZy);
    }

    @SuppressLint("HandlerLeak")
    private class MyHandler extends Handler {

        @Override
        public void handleMessage(Message msg) {
            switch (msg.what) {
                case SHOWPROGRESSDIALOG:
                    dialog = CustomeProgressDialog.show(XkActivity.this, "正在加载数据...");
                    break;
                case DISMISSDIALOG:
                    if (dialog != null) {
                        dialog.dismiss();
                    }
                    break;
                case LOADINGFAILED:
                    myHandler.sendEmptyMessage(DISMISSDIALOG);
                    String failureMsg = msg.getData().getString("msg");
                    MyApplication.getInstance().showShortToast(failureMsg);
                    break;
                case LOADINGXKINFOFINISHED:
                    myHandler.sendEmptyMessage(DISMISSDIALOG);
                    lbGxMax.setText("公选课最低毕业要求:" + xkInfo.getLbGxMax());
                    lbZyMax.setText("专选课最低毕业要求:" + xkInfo.getLbZyMax());
                    lbGxxf.setText("公选课已经通过学分:" + xkInfo.getLbGxxf());
                    lbGxbjgxf.setText("公选课不及格学分:" + xkInfo.getLbGxbjgxf());
                    lbGx.setText("公选课可选学分:" + xkInfo.getLbGx());
                    lbGxCount.setText("本学期公选已选门次:" + xkInfo.getLbGxCount());
                    lbGxMaxCount.setText("本学期公选可选门次:" + xkInfo.getLbGxMaxCount());
                    lbZyxf.setText("专业选修已通过学分:" + xkInfo.getLbZyxf());
                    lbZybjgxf.setText("专业选修不及格学分:" + xkInfo.getLbZybjgxf());
                    lbZy.setText("专业选修可选学分:" + xkInfo.getLbZy());
                    selYear.setText(xkInfo.getSelectedYear() + "年");
                    selTerm.setText("1".equals(xkInfo.getSelectedTerms()) ? "上学期" : "下学期");
                    selXiaoqu.setText("请选择校区");
                    xkInfo.setSelectedXiaoqu("1");
                    break;
                case LOADINGSELECTEDCOURCEFINISHED:
                    myHandler.sendEmptyMessage(DISMISSDIALOG);
                    Intent intent = new Intent();
                    intent.setClass(XkActivity.this, SelectedCourseActivity.class);
                    intent.putExtra("courses", (Serializable) courses);
                    intent.putExtra("year", selYear.getText());
                    intent.putExtra("term", selTerm.getText());
                    startActivity(intent);
                    break;
                case LOADINGGXKLISTFINISHED:
                    myHandler.sendEmptyMessage(DISMISSDIALOG);
                    Intent itt = new Intent();
                    itt.setClass(XkActivity.this, GxkActivity.class);
                    itt.putExtra("xkInfo", (Serializable) gxkInfo.get("xkInfo"));
                    itt.putExtra("gxkList", (Serializable) gxkInfo.get("gxkList"));
                    startActivity(itt);
                default:
                    break;
            }
        }
    }

    public void onButtonClick(View v) {
        switch (v.getId()) {
            case R.id.selYear:
                if (xkInfo == null || selYear.getText() == null) {
                    return;
                }
                new AlertDialog.Builder(XkActivity.this).setTitle("请选择年份").setItems(xkInfo.getYears(), new OnClickListener() {

                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        // TODO Auto-generated method stub
                        selYear.setText(xkInfo.getYears()[which] + "年");
                    }
                }).show();
                break;
            case R.id.selTerm:
                if (xkInfo == null || selTerm.getText() == null) {
                    return;
                }
                final String[] terms = new String[]{"上学期", "下学期"};
                new AlertDialog.Builder(XkActivity.this).setTitle("请选择学期").setItems(terms, new OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        // TODO Auto-generated method stub
                        selTerm.setText(terms[which]);
                    }
                }).show();
                break;
            case R.id.selXiaoqu:
                if (xkInfo == null || selXiaoqu.getText() == null) {
                    return;
                }
                new AlertDialog.Builder(XkActivity.this).setTitle("请选择校区").setItems(xkInfo.getXiqoqus(), new OnClickListener() {

                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        // TODO Auto-generated method stub
                        selXiaoqu.setText(xkInfo.getXiqoqus()[which]);
                        xkInfo.setSelectedXiaoqu((which + 1) + "");
                    }
                }).show();
                break;
            case R.id.bt_selectedCourse:
                if (MainActivity.student == null) {
                    Toast.makeText(getApplicationContext(), "您还没登录!请返回登录!", Toast.LENGTH_SHORT).show();
                    return;
                }
                getSelectedCourse();
                break;
            case R.id.bt_gongxuanke:
                getGxkList();
                break;
            case R.id.bt_zhuanxuanke:
                Toast.makeText(getApplicationContext(), "对不起,由于暂时不能选课,该功能暂时没有完成.请关注下个版本", Toast.LENGTH_LONG).show();
                break;
            default:
                break;
        }
    }

    private void getXkInfo() {
        myHandler.sendEmptyMessage(SHOWPROGRESSDIALOG);
        StudentServiceImpl.getInstance().getXkInfo(new BusinessHttpCallBack() {
            @Override
            public void onFailure(byte[] responseBody, String failuremsg, boolean isNoticeDialog) {
                Message msg = myHandler.obtainMessage();
                msg.what = LOADINGFAILED;
                msg.getData().putString("msg", failuremsg);
                myHandler.sendMessage(msg);
            }

            @Override
            public void onSuccess(byte[] responseBody, String successMsg, boolean isNoticeDialog, Object obj) {
                xkInfo = (XkInfo) obj;
                myHandler.sendEmptyMessage(LOADINGXKINFOFINISHED);
            }
        });
    }

    private void getGxkList() {
        myHandler.sendEmptyMessage(SHOWPROGRESSDIALOG);
        StudentServiceImpl.getInstance().getGxk(xkInfo.getSelectedXiaoqu(), new BusinessHttpCallBack() {
            @Override
            public void onFailure(byte[] responseBody, String failuremsg, boolean isNoticeDialog) {
                Message msg = myHandler.obtainMessage();
                msg.what = LOADINGFAILED;
                msg.getData().putString("msg", "抱歉，网络错误或者现在还不是选课的时候！");
                myHandler.sendMessage(msg);
            }

            @SuppressWarnings("rawtypes")
            @Override
            public void onSuccess(byte[] responseBody, String successMsg, boolean isNoticeDialog, Object obj) {
                gxkInfo = (Map) obj;
                myHandler.sendEmptyMessage(LOADINGGXKLISTFINISHED);
            }
        });
    }

    private void getSelectedCourse() {
        String year = selYear.getText().toString().substring(0, 4);
        String term = "上学期".equals(selTerm.getText().toString()) ? "1" : "2";
        myHandler.sendEmptyMessage(SHOWPROGRESSDIALOG);
        StudentServiceImpl.getInstance().getXkInfo(year, term, xkInfo.getSelectedXiaoqu(), new BusinessHttpCallBack() {
            @Override
            public void onFailure(byte[] responseBody, String failuremsg, boolean isNoticeDialog) {
                Message msg = myHandler.obtainMessage();
                msg.what = LOADINGFAILED;
                msg.getData().putString("msg", failuremsg);
                myHandler.sendMessage(msg);
            }

            @SuppressWarnings("unchecked")
            @Override
            public void onSuccess(byte[] responseBody, String successMsg, boolean isNoticeDialog, Object obj) {
                courses = (List<Course>) obj;
                L.d("判断courses是否为空0" + (courses == null));
                myHandler.sendEmptyMessage(LOADINGSELECTEDCOURCEFINISHED);
            }
        });
    }
}
